/*	$Id: private.h 1119 2010-12-12 21:29:18Z phrakt $ */
/*
 * Copyright (c) 2003 Jean-Francois Brousseau <phrakt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


#ifndef _HTTP_PRIVATE_H_
#define _HTTP_PRIVATE_H_

#include <sys/types.h>

#include <netinet/in.h>

#include <stdint.h>

#include <canopy/buf.h>
#include <canopy/sys.h>
#include <canopy/htab.h>
#include <canopy/thread.h>

#include "uri.h"
#include "addr.h"
#include "http.h"
#include "sock.h"
#include "vhost.h"
#include "session.h"


extern const char* const http_req_methods[8];


struct http_vhost {
	char                 *vh_name;
	enum http_vhost_type  vh_type;
	int                   vh_flags;
	int                   vh_refcnt;	/* reference count */
	char                 *vh_docroot;	/* document root directory */

	union {
		char            *vh_hostname;
		http_sockaddr_t  vh_addr;
	} vh_addr;

	cnp_rwlock_t          vh_rwlock;	/* read-write lock */
};



struct http_sock {
	enum http_sock_type   hs_type;
	int                   hs_fd;
	int                   hs_flags;
	enum http_sock_state  hs_state;
	int                   hs_refcnt;	/* reference count */

	http_session_t       *hs_session;	/* associated session */

	/* local address */
	http_sockaddr_t       hs_laddr;
	char                 *hs_laddr_str;	/* string representation */

	/* remote address */
	http_sockaddr_t       hs_raddr;
	char                 *hs_raddr_str;	/* string representation */

	void                 *hs_rbuf;		/* read buffer */
	size_t                hs_rbuflen;

	cnp_buf_t            *hs_wbuf;		/* write buffer */

	cnp_mutex_t           hs_lock;
};


struct http_session {
	int                   hs_id;
	int                   hs_flags;
	enum http_sess_state  hs_state;
	int                   hs_refcnt;	/* reference count */

	http_sock_t          *hs_sock;

	char                 *hs_user_agent;

	cnp_htab_t           *hs_attr;		/* session attributes */

	time_t                hs_ctime;		/* time of creation */
	time_t                hs_atime;		/* time of last access */
	time_t                hs_timeout;	/* timeout delay */

	/* statistics */
	size_t                hs_bytes_out;
	size_t                hs_bytes_in;
	int                   hs_msg_out;
	int                   hs_msg_in;

	cnp_mutex_t           hs_lock;
};


struct http_uri {
	char     uri_scheme[HTTP_URI_MAXSCHEMELEN];
	char    *uri_user;
	char    *uri_host;
	int      uri_port;

	char    *uri_path;
	char    *uri_fragment;
	char    *uri_query;
};


/*
 * HTTP client structure
 *
 */
struct http_client {
	enum http_version  cl_version;
	char              *cl_user_agent;

	http_sock_t       *cl_sock;

	cnp_rwlock_t       cl_rwlock;
};



/* a shorter version for internal library calls */
#define http_log(l, ...)	cnp_log(http_logchan, l, __VA_ARGS__)
#define http_log_trace(...)	http_log(CNP_LOG_TRACE, __VA_ARGS__)
#define http_log_debug(...)	http_log(CNP_LOG_DEBUG, __VA_ARGS__)
#define http_log_info(...)	http_log(CNP_LOG_INFO, __VA_ARGS__)
#define http_log_notice(...)	http_log(CNP_LOG_NOTICE, __VA_ARGS__)
#define http_log_warn(...)	http_log(CNP_LOG_WARN, __VA_ARGS__)
#define http_log_err(...)	http_log(CNP_LOG_ERR, __VA_ARGS__)
#define http_log_errno(...)	http_log(CNP_LOG_ERRNO, __VA_ARGS__)
#define http_log_crit(...)	http_log(CNP_LOG_CRIT, __VA_ARGS__)


int      http_media_init     (void);
void     http_media_cleanup  (void);

#endif /* _HTTP_PRIVATE_H_ */
